package com.wanger.swa.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanger.swa.dto.buildRecord.req.BuildRecordAddReq;
import com.wanger.swa.dto.buildRecord.req.BuildRecordUpdateReq;
import com.wanger.swa.dto.buildRecord.resp.BuildRecordResp;
import com.wanger.swa.domain.po.BuildRecordPO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wanger
 * @Date: 2024/9/16 17:40
 * @Description: 构建记录
 */
@Component
public class BuildRecordConvert {

    private BuildRecordPO baseVerify(BuildRecordPO po) {
        //进行通用数据验证
        return po;
    }

    public BuildRecordPO toSaveEntity(BuildRecordAddReq addDTO) {
        return baseVerify(BeanUtil.copyProperties(addDTO, BuildRecordPO.class));
    }

    public BuildRecordPO toUpdateEntity(BuildRecordUpdateReq updateDTO) {
        return baseVerify(BeanUtil.copyProperties(updateDTO, BuildRecordPO.class));
    }

    public BuildRecordResp toDetailsDTO(BuildRecordPO buildRecord) {
        return toListDTO(CollUtil.toList(buildRecord))
                .stream()
                .findFirst()
                .orElse(null);
    }

    public List<BuildRecordResp> toListDTO(List<BuildRecordPO> list) {
        return BeanUtil.copyToList(list, BuildRecordResp.class);
    }

    public IPage<BuildRecordResp> toPageDTO(IPage<BuildRecordPO> page) {
        return new Page<BuildRecordResp>()
                .setRecords(toListDTO(page.getRecords()))
                .setTotal(page.getTotal())
                .setSize(page.getSize())
                .setCurrent(page.getCurrent())
                .setPages(page.getPages());
    }
}
